﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Bankeren.Engine.Cards
{
    /// <summary>
    /// This class represents the rank of a playingcard.
    /// </summary>
    [DataContract]
    public class Rank : IComparable<Rank>
    {
        #region Fields

        private string name;
        private char symbol;
        private int value;

        #endregion

        #region Ranks

        /// <summary>
        /// Two rank.
        /// </summary>
        public static readonly Rank Two = new Rank("Two", '2', 2);

        /// <summary>
        /// Three rank.
        /// </summary>
        public static readonly Rank Three = new Rank("Three", '3', 3);

        /// <summary>
        /// Four rank.
        /// </summary>
        public static readonly Rank Four = new Rank("Four", '4', 4);

        /// <summary>
        /// Five rank.
        /// </summary>
        public static readonly Rank Five = new Rank("Five", '5', 5);

        /// <summary>
        /// Six rank.
        /// </summary>
        public static readonly Rank Six = new Rank("Six", '6', 6);

        /// <summary>
        /// Seven rank.
        /// </summary>
        public static readonly Rank Seven = new Rank("Seven", '7', 7);

        /// <summary>
        /// Eight rank.
        /// </summary>
        public static readonly Rank Eight = new Rank("Eight", '8', 8);

        /// <summary>
        /// Nine rank.
        /// </summary>
        public static readonly Rank Nine = new Rank("Nine", '9', 9);

        /// <summary>
        /// Ten rank.
        /// </summary>
        public static readonly Rank Ten = new Rank("Ten", 't', 10);

        /// <summary>
        /// Jack rank.
        /// </summary>
        public static readonly Rank Jack = new Rank("Jack", 'j', 11);

        /// <summary>
        /// Queen rank.
        /// </summary>
        public static readonly Rank Queen = new Rank("Queen", 'q', 12);

        /// <summary>
        /// King rank.
        /// </summary>
        public static readonly Rank King = new Rank("King", 'k', 13);

        /// <summary>
        /// Ace rank.
        /// </summary>
        public static readonly Rank Ace = new Rank("Ace", 'a', 14);

        /// <summary>
        /// Back rank.
        /// </summary>
        public static readonly Rank Back = new Rank("Back", 'b', 0);

        /// <summary>
        /// List of all ranks.
        /// </summary>
        public static readonly Rank[] List = { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace };

        #endregion

        #region Properties

        /// <summary>
        /// The ranks name.
        /// </summary>
        [DataMember]
        public string Name
        {
            get
            {
                return this.name;
            }
            private set
            {
                this.name = value;
            }
        }

        /// <summary>
        /// The ranks symbol.
        /// </summary>
        [DataMember]
        public char Symbol
        {
            get
            {
                return this.symbol;
            }
            private set
            {
                this.symbol = value;
            }
        }

        /// <summary>
        /// The ranks value.
        /// </summary>
        [DataMember]
        public int Value
        {
            get
            {
                return this.value;
            }
            private set
            {
                this.value = value;
            }
        }

        #endregion

        #region Ctor

        /// <summary>
        /// Private constructor for rank.
        /// </summary>
        /// <param name="name">The ranks name.</param>
        /// <param name="symbol">The ranks symbol.</param>
        /// <param name="value">The ranks value.</param>
        private Rank(string name, char symbol, int value)
        {
            this.Name = name;
            this.Symbol = symbol;
            this.Value = value;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Override of the default ToString().
        /// </summary>
        /// <returns>The ranks name.</returns>
        public override string ToString()
        {
            return this.Name;
        }

        /// <summary>
        /// For comparing ranks. (For sorting purposes)
        /// </summary>
        /// <param name="other">The rank for comparing.</param>
        /// <returns>A negative int if this rank is smaller,
        /// 0 if the ranks are the same and a positive int if this rank is bigger.</returns>
        public int CompareTo(Rank other)
        {
            return this.value - other.value;
        }

        public override bool Equals(object obj)
        {
            Rank rank = obj as Rank;
            if (rank == null)
                return false;
            if (this.symbol == rank.symbol)
                return true;
            return false;
        }

        #endregion
    }
}
